﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TryHelp
{
    public class MachineCode
    {
        static Encoding enc = Encoding.Unicode;
        static string cfgFilePath = string.Empty;
        public static string Value()
        {
            string ClientId = Guid.NewGuid().ToString("N").ToUpper();
            cfgFilePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\z2e0y1a7.dll";
            if (!File.Exists(cfgFilePath))
            {
                SaveCfgFile(ClientId);
            }
            else
            {
                byte[] allData = File.ReadAllBytes(cfgFilePath);
                if (allData.Length != 1024)
                {
                    ClientId = Guid.NewGuid().ToString("N").ToUpper();
                    SaveCfgFile(ClientId);
                }
                else
                {
                    ClientId = enc.GetString(allData, 255, 64);
                }
            }
            FileInfo fi = new FileInfo(cfgFilePath);
            fi.Attributes = FileAttributes.Normal;
            return ClientId;
        }

        static void SaveCfgFile(string clientid)
        {
            byte[] allData = new byte[1024];
            byte[] rand = enc.GetBytes(Guid.NewGuid().ToString("N").ToUpper());
            byte[] guid = enc.GetBytes(clientid);
            rand.CopyTo(allData, 0);
            guid.CopyTo(allData, 255);
            enc.GetBytes("DNet.Client.WH").CopyTo(allData, 767);
            File.WriteAllBytes(cfgFilePath, allData);
        }
    }
}
